package com.xiyun.msm.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;

public class AliMsgUtil {

    //阿里云短信服务
    public static boolean sendMsg(String phone,String code){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", Constant.KEYID, Constant.KEYSECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        //设置相关参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //发送短信相关参数
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);                //设置要发送的手机号
        request.putQueryParameter("SignName","签名的名称");          //申请的签名名称
        request.putQueryParameter("TemplateCode","模板CODE");       //申请的模板CODE
        request.putQueryParameter("TemplateParam", JSON.toJSONString(new HashMap<String,Object>().put("code",code)));   //验证码数据,需要转为json
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}
